package pers.qianyu.controller;

import lombok.extern.slf4j.*;
import org.apache.rocketmq.spring.core.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.domain.*;
import pers.qianyu.service.*;

@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/order/product/{pid}")
    public Order order(@PathVariable Integer pid) {
        log.info("收到{}号商品的下单请求，接下来调用订单微服务查询订单", pid);
        Product product = productService.findById(pid);
        log.info("查询到商品号{}的商品信息：{}", pid, product.toString());

        Order order = new Order();
        order.setUid(1);
        order.setUsername("小雨");
        order.setPid(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.createOrder(order);
        log.info("新建订单：{}", order.toString());

        // 下单成功之后,将消息放到mq中
        rocketMQTemplate.convertAndSend("order-topic", order);

        return order;
    }
}